﻿package fwk.form.elements 
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.display.MovieClip;
	import flash.events.FocusEvent;
	import flash.events.TextEvent;
	

	public class QuestionText extends AbstractQuestion
	{
		
		private var _tTexte:TextField;
		private var _reponse:AbstractReponse;
		private var _valueInitial:String = "";
		
		
		
		public function QuestionText(mc:MovieClip = null) 
		{

			super(mc);
			
		}
		public function init(xmlConfig:XML, champsTexte:TextField)
		{
			
			super._init(xmlConfig);
			
			_reponse 		= new AbstractReponse();
			
			_tTexte 		= champsTexte;
			_valueInitial 	= _tTexte.text;

			
			_tTexte.addEventListener(FocusEvent.FOCUS_IN, _onInputFocus);
			_tTexte.addEventListener(FocusEvent.FOCUS_OUT, _onInputFocusOut);
			_tTexte.addEventListener(TextEvent.TEXT_INPUT, _onTextChange);
			

			
			
		}
		
		private function _onTextChange(e:TextEvent):void 
		{
			hideError();
		}
		

		override public function set tabIndex(value:int):void 
		{
			trace("QuestionText tabIndex " + value);
			
			
			
			if (value == -1) {
				_tTexte.tabEnabled = false;
			}else {
				_tTexte.tabEnabled = true;
			}
			_tTexte.tabIndex = value;
		}
		
		private function _onInputFocusOut(e:FocusEvent):void 
		{
			if (shapeFocus) _mc.removeChild(spriteFocus);
		}
		
		private function _onInputFocus(e:FocusEvent):void 
		{

			hideError();
			if (shapeFocus) {
				spriteFocus.graphics.clear();
				spriteFocus.graphics.beginFill(colorShapeFocus, 0.2);
				spriteFocus.graphics.drawRect(  _mc.input.x - 3,  _mc.input.y - 3, _mc.input.width + 6, _mc.input.height + 6);
				_mc.addChildAt(spriteFocus, 0);
			}

			
			
		}
		override public function reset():void 
		{
			
			hideError();
			
			_reponse.value 			= _valueInitial;
			_tTexte.text 			= _valueInitial;
			
		}
		override public function get reponse():AbstractReponse
		{
			_reponse.id 			= id;
			_reponse.value 			= _tTexte.text;
			_reponse.valueInitial 	= _valueInitial;
			return _reponse;
		}
		
		
	}
	
}